package com.my.vote.pst.user;

import android.text.TextUtils;

import com.my.vote.VoteApplication;
import com.my.vote.entity.EUser;
import com.my.vote.entity.req.ReqRegister;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.RxBusEventManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * 功能描述:注册
 *
 * @author
 * @date 2018/4/12
 */
public class PstRegister extends AbstractPresenter<IPstRegister.IVRegister> implements IPstRegister {
    @Override
    public void register(final ReqRegister reqRegister) {

        if (reqRegister == null) {
            getView().showToast("Please fill in the relevant user information");
            return;
        }
        if (TextUtils.isEmpty(reqRegister.getNumber())) {
            getView().showToast("Please enter the account number");
            return;
        }
        if (TextUtils.isEmpty(reqRegister.getPwd())) {
            getView().showToast("Please input a password");
            return;
        }
        if (!reqRegister.isAgree()) {
            getView().showToast("Please agree with the user agreement");
            return;
        }
        this.getView().showLoading(false);
        BmobQuery<EUser> query = new BmobQuery<>();
        query.addWhereEqualTo("account", reqRegister.getNumber());
        query.findObjects(AppCacheManager.getApplication(),new FindListener<EUser>() {
            @Override
            public void onSuccess(List<EUser> list) {
                if (list == null || list.isEmpty()) {
                    final EUser user = new EUser();
                    user.setUserName(reqRegister.getNumber());
                    user.setAccount(reqRegister.getNumber());
                    user.setPassword(reqRegister.getPwd());

                    user.save(AppCacheManager.getApplication(),new SaveListener() {
                        @Override
                        public void onSuccess() {
                            getView().hideLoading();
                            // 保存用户信息
                            AppCacheManager.newInstance().putCache(true, VoteApplication.USER,user);
                            RxBusEventManager.postEvent(user);
                            getView().onSuccess();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            getView().hideLoading();
                            getView().onError("User registration failure!");
                        }
                    });
                } else {
                    getView().hideLoading();
                    getView().onError("The user has already existed!");
                }
            }

            @Override
            public void onError(int i, String s) {
                getView().hideLoading();
                getView().onError("User registration failure!");
            }
        });
    }
}
